package cn.hxzy.service.impl;

import cn.hxzy.common.R;
import cn.hxzy.entity.User;
import cn.hxzy.mapper.UserMapper;
import cn.hxzy.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yuankan.WANG
 * @since 2022-01-13
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private StringRedisTemplate srt;



    @Override
    public R update(String userName) {

        if (redisTemplate.hasKey(userName)){
            redisTemplate.delete(userName);
        }
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("user_name",userName);
        User user = userMapper.selectOne(wrapper);
        user.setStatus(1);
        int i = userMapper.updateById(user);
        if (i>0){
            return R.ok();
        }
        return R.ok();
    }

    @Override
    public R sel() {
        QueryWrapper wrapper=new QueryWrapper();
        List<User> list = userMapper.selectList(wrapper);
        if (!Objects.isNull(list)) {
            Long user = redisTemplate.opsForList().leftPushAll("user", list);
            return R.ok().data("item",list);
        }

        return R.error();
    }
//        User user = userMapper.selectById(userId);
//        if (Objects.isNull(user)){
//            return R.error();
//        }
//        User user1 = user.setStatus(1);
//        int i = userMapper.updateById(user1);
//        if (i>0){
//            srt.opsForHash().put("user","userId",user.getUserId());
//            srt.opsForHash().put("user","userName",user.getUserId());
//            srt.opsForHash().put("user","status",user.getStatus());
//            if (user.getStatus()==1){
//               // redisTemplate.delete();
//            }
//            return R.ok().code(20000).data("item",i).message("修改成功");
//        }
//        return null;

}
